<template>
	<view class="safety storesTransfer">
		<view class=" topHeaderBox between navBox" :style="{paddingTop : `${systemBarHeight+10}px`}">
			<uni-icons type="left" color="#fff" size="23" @click="goBack(100)"></uni-icons>
			<view>收益分配</view>
			<view style="font-size: 28rpx;">
			</view>
		</view>
		<!-- 主要内容区 -->
		<view class="mainBox">
			<view class="dataBox">
				<view style="display: flex; flex-direction: row; align-items: center;justify-content: center;">
					<view
						style="flex:1; text-align: center; display: flex; align-items: center;justify-content: center;">
						<view class="btn" v-if="!isEditing" @click="this.isEditing = true">
							修改
						</view>
						<view class="btn" v-if="isEditing" @click="this.isEditing = false;onGetAgencyList()">
							取消
						</view>
					</view>
					<view
						style="flex:1; text-align: center;display: flex; align-items: center;justify-content: center;">
						<view class="btn_green" v-if="isEditing" @click="submit">
							保存
						</view>
					</view>
				</view>
				<view style="height: 1px; background-color: #eee; margin: 20rpx 0;"></view>
				<view style="display: flex; flex-direction: row; align-items: center;justify-content: center;">
					<view class="flex-center" style="flex:1;flex-direction: column;margin-left: 30rpx;">
						<view>本次发放总额</view>
						<view class="dataVal" v-if="!isEditing">
							{{ total_money }}
						</view>
						<view class="dataVal" v-else>
							<input type="digit" class="input"
								style="text-align: center; border-bottom: solid 1px #999;" v-model="total_money" />
						</view>
					</view>
					<view class="flex-center" style="flex:1;flex-direction: column;margin-left: 30rpx;">
						<view>昨日收益</view>
						<view class="dataVal">{{daili_income_yestoday}}</view>
					</view>
				</view>
			</view>
			<!-- 列表 -->
			<view class="listBigBox">
				<view style="height: 1px; background-color: #eee; margin: 20rpx 0;"></view>
				<view class="between hintBox" style="display: flex; flex-direction: row;">
					<view style="flex: 1; text-align: center;">昨日前20名用户ID</view>
				</view>
				<view class="between listBox"
					style="display: flex; flex-direction: row; justify-content: flex-start;flex-wrap: wrap;">
					<view v-for="(item,i) in list" :key="i" style="font-size: 26rpx;">{{item.mid}}</view>
				</view>
			</view>
			<view class=" flex-center addressImgBox" v-if="list.length==0">
				<image src="https://wx.2024csnl.com/static/noData.png"></image>
				暂无数据~
			</view>

			<!-- 列表 -->
			<view class="listBigBox">
				<view style="height: 1px; background-color: #eee; margin: 20rpx 0;"></view>
				<view class="between hintBox" style="display: flex; flex-direction: row;">
					<view style="flex: 0.6; text-align: center;">序号</view>
					<view style="flex: 1.5; text-align: center;">用户ID号码</view>
					<view style="flex: 1; text-align: center;">操作</view>
				</view>
				<view class="between listBox" v-for="(item,i) in midlist" :key="i"
					style="display: flex; flex-direction: row;">
					<view class="tit1">{{i+1}}</view>
					<view class="tit2" v-if="!isEditing">{{item.mid}}</view>
					<view class="tit2" style="padding: 0 30rpx;" v-else>
						<input type="number" class="input" style="text-align: center; border-bottom: solid 1px #999;"
							v-model="item.mid" />
					</view>
					<view class="tit">
						<view v-if="isEditing" class="btn_del" @click="del(i)">删除</view>
					</view>
				</view>
				<view v-if="isEditing" class="btn_add" @click="this.midlist.push({mid: ''})">增加</view>
			</view>

			<view v-if="!isEditing" class="btn_send" @click="send()">发放</view>
			<uni-popup ref="popup" type="bottom">
				<u-calendar type="dateRange" :startYear="endYear-1" :endYear="endYear" @change="onChangeTime">
				</u-calendar>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import selectPicker from "../../../components/selectPicker/selectPicker.vue"
	import {
		getAgencyList,
		getAgencyMsg
	} from "../../../api/index.js"
	export default {
		components: {
			selectPicker
		},
		data() {
			return {
				pernum: '',
				isEditing: false,
				total_money: 0,
				daili_income_yestoday: 0,
				list: [],
				midlist: [],
				totalPage: '',
				noMoreData: false,
				pagenum: 1,
				agencyMsg: {}, //代理数据 dlid-身份: 4区代理、5市代理、6省代理
				selectTime: '时间', //筛选时间
				selectTimeList: [],
				selectCity: '',
				selectArea: '',
				array: [], //区代理数据
				index: 0,
				dmid: '',
				endYear: new Date().getFullYear(),
				startTime: null,
				endTime: null,
				systemBarHeight: getApp().globalData.systemBarHeight,
			}
		},
		onLoad(opt) {
			this.onGetAgencyList()
		},
		methods: {
			onRefirsh() {
				this.pagenum = 1
				this.onGetAgencyList()
			},
			del(i) {
				this.midlist.splice(i, 1);
			},

			async send() {
				if (this.total_money <= 0) {
					this.showToast("请先配置本次发放总额")
					return;
				}
				const res = await getAgencyList(`s=/ApiZwyRequest/agentfenpeisend`, {})
				const {
					data,
					status,
					msg
				} = res.data
				if (status == 1) {
					this.isEditing = false;
					this.onGetAgencyList();
					this.showToast(msg)
				} else {
					this.onGetAgencyList();
					this.isEditing = false;
					this.showToast(msg)
				}
			},
			async submit() {
				const res = await getAgencyList(`s=/ApiZwyRequest/agentfenpeisubmit`, {
					total_money: this.total_money,
					mids: this.midlist.map(x => x.mid).join(",")
				})
				console.log(res)
				const {
					data,
					status,
					pernum,
					msg
				} = res.data
				if (status == 1) {
					this.isEditing = false;
					this.onGetAgencyList();
					this.showToast(msg)
				} else {
					this.onGetAgencyList();
					this.isEditing = false;
					this.showToast(msg)
				}
			},
			// 代理列表
			async onGetAgencyList() {
				const res = await getAgencyList(`s=/ApiZwyRequest/agentfenpeiinfo`, {
					dmid: this.dmid
				})
				const {
					data,
					datalist,
					status,
					midlist,
					msg
				} = res.data
				if (status == 1) {

					const {
						total_money,
						daili_income_yestoday
					} = data;

					this.total_money = total_money
					this.daili_income_yestoday = daili_income_yestoday

					if (midlist) {
						this.midlist = midlist
					}

					this.list = datalist
				} else {
					this.showToast(msg)
				}
			},
			bindPickerChange(e) {
				this.index = e.detail.value
				this.selectArea = this.array[this.index]
				this.onRefirsh()
			},
			//改变区域
			change(result) {
				this.selectCity = result.data[0].name,
					this.selectArea = result.data[1].name,
					// console.log(result, "rrr")
					this.selectTimeList = []
				this.selectTime = '时间'
				this.onRefirsh()
			},
			// 代理信息
			async onGetAgencyMsg() {
				const res = await getAgencyList(`s=/ApiZwyRequest/agentinfo`, {
					dmid: this.dmid
				})
				const {
					data,
					status,
					msg
				} = res.data
				// console.log(res, "代理信息")

				if (status == 1) {
					this.agencyMsg = data
					if (this.agencyMsg.dlid == 5) {
						data.agentselect1.forEach((item, i) => {
							this.array.push(item.name)
						})
						this.array[0] = '全部'
					}

				} else {
					this.showToast(msg)
				}
			},

			onChangeTime(e) {
				this.selectTimeList = e
				// console.log(this.selectTimelist, '')
				this.selectTime = e[0].result
				this.onRefirsh()
				setTimeout(() => {
					this.close('popup')
				}, 100)

			},
			open(n) {
				this.$refs[n].open();
			},
			close(n) {
				this.$refs[n].close();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.addressImgBox {
		flex-direction: column;
		color: #999;
		padding-top: 20%;

		image {
			width: 400rpx;
			height: 400rpx;
		}
	}

	.listBigBox {
		padding: 0 10rpx;
		background: #fff;

		image {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.listBox {
			padding: 30rpx 0;
			align-items: center;
			color: #333333;

			.name {
				width: 150rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.tit1 {
				flex: 0.6;
				text-align: center;
				font-size: 26rpx;
			}

			.tit {
				flex: 1;
				text-align: center;
				font-size: 26rpx;
			}

			.tit2 {
				flex: 1.5;
				text-align: center;
				font-size: 26rpx;
			}
		}

		.hintBox {
			color: #333333;
			padding: 20rpx 0;
			font-size: 24rpx;
			font-weight: bold;
		}
	}

	.stat {
		font-size: 32rpx;
		color: #F17816;
		font-weight: 500;
		background: #FFF2F0;
		padding: 23rpx 38rpx;
	}

	.line {
		height: 100rpx;
		width: 1px;
		background: #F5F5F5;
		margin-left: 40rpx;
	}

	.oneList {
		padding: 40rpx 30rpx;
		background: #fff;
		display: grid;
		grid-template-columns: 25% 75%;
		grid-gap: 0 20rpx;
		margin-bottom: 20rpx;

		image {
			width: 180rpx;
			height: 180rpx;
			border-radius: 10rpx;
		}

		.rightBox {
			padding: 0 30rpx;
			font-size: 30rpx;
			line-height: 48rpx;

			.tit {
				font-weight: bold;
				color: #333333;
				font-size: 32rpx;
				margin-bottom: 30rpx;
			}

			.site {
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}

			text {
				color: #999999;
			}
		}
	}

	.filterBigBox {
		padding: 40rpx 30rpx;
		z-index: 0;
		background: #fff;
		display: grid;
		grid-template-columns: 70% 30%;

	}

	.downIcon {
		z-index: 0;
		width: 24rpx;
		height: 16rpx;
		margin-left: 15rpx;
	}

	.mainBox {
		position: relative;
		top: 150rpx;
		border-radius: 30rpx;
		width: 98%;
		margin: auto;
		z-index: 1999;
		background: #fff;
	}

	.dataBox {
		align-items: center;
		color: #B3B3B3;
		font-size: 26rpx;
		padding: 30rpx 10rpx 0 10rpx;

		.dataVal {
			font-size: 30rpx;
			color: #FF9320;
			margin-top: 15rpx;
			font-weight: bold;
		}

		.btn {
			width: 200rpx;
			text-align: center;
			padding: 20rpx 20rpx;
			background: #EC2E2F;
			border-radius: 10rpx;
			font-size: 25rpx;
			color: #fff;
			font-weight: bold;
		}

		.btn_green {
			width: 200rpx;
			text-align: center;
			padding: 20rpx 20rpx;
			background: green;
			border-radius: 10rpx;
			font-size: 25rpx;
			color: #fff;
			font-weight: bold;
		}
	}

	.storesTransfer {
		min-height: 100vh;
	}

	.topHeaderBox {
		padding: 30rpx;
		height: 270rpx;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		z-index: 999;
		border-radius: 0 0 30rpx 30rpx;
		display: flex;
		background: linear-gradient(0deg, #FFA025, #41644A);
	}

	.navBox {
		font-size: 34rpx;
		font-weight: bold;
		color: #fff;
	}

	.btn_del {
		text-align: center;
		padding: 10rpx 20rpx;
		background: #EC2E2F;
		border-radius: 10rpx;
		font-size: 22rpx;
		color: #fff;
		font-weight: bold;
	}

	.btn_add {
		margin-top: 20rpx;
		text-align: center;
		padding: 20rpx 20rpx;
		background: #ccc;
		border-radius: 10rpx;
		font-size: 22rpx;
		color: #333;
		font-weight: bold;
	}

	.btn_send {
		margin-top: 20rpx;
		text-align: center;
		padding: 20rpx 20rpx;
		background: green;
		border-radius: 10rpx;
		font-size: 22rpx;
		color: #fff;
		font-weight: bold;
	}
</style>